package com.smedi.ismedi.constructionproject.core.domain.service.impl;

import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSection;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfig;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigOrganizationRepository;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionRepository;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionConfigDomainService;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionDomainService;
import com.smedi.ismedi.constructionproject.core.domain.service.ProjectBasicDomainService;
import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 9:22
 */
@Slf4j
@Service
@AllArgsConstructor
public class BidSectionDomainServiceImpl implements BidSectionDomainService {
    private final BidSectionRepository bidSectionRepository;

    private final ProjectBasicDomainService projectBasicDomainService;

    private final BidSectionConfigDomainService bidSectionConfigDomainService;

    private final BidSectionConfigOrganizationRepository bidSectionConfigOrganizationRepository;

    @Override
    public BidSection saveInfo(BidSection data) {
        BidSection basic;
        if (data.getBidSectionId() != null) {
            basic = bidSectionRepository.selectByBidSectionId(data.getBidSectionId());
            if (basic == null) {
                throw LogicException.raise(CustomError.BID_SECTION_NOT_EXIST);
            }
        } else {
            basic = new BidSection();
        }

        basic.setProjectId(data.getProjectId())
                .setContractAmount(data.getContractAmount())
                .setBidSectionCode(data.getBidSectionCode())
                .setBidSectionShortName(data.getBidSectionShortName())
                .setBidSectionName(data.getBidSectionName());

        // 判断当前项目是否存在
        boolean validate = projectBasicDomainService.validateProject(basic.getProjectId());
        if (!validate) {
            throw LogicException.raise(CustomError.PROJECT_NOT_AVAILABLE);
        }

        // 与前端确认，后端不校验、不默认生成配置信息项，只校验配置项中的组织信息是否符合业务逻辑
        // 配置项的初始化由前端控制；后端可判断传入的数据是否包含了默认配置类型。
        List<BidSectionConfig> bidSectionConfigs = data.getBidSectionConfigs();
        bidSectionConfigDomainService.validate(bidSectionConfigs, basic.getProjectId());

        // 若校验通过，则保存配置项数据
        if (basic.getBidSectionId() != null) {
            bidSectionConfigDomainService.cleanBidSectionConfig(basic.getBidSectionId());
        }

        // 保存数据
        BidSection domain = (basic.getBidSectionId() == null) ? bidSectionRepository.doInsert(basic) : bidSectionRepository.doUpdate(basic);
        domain.setBidSectionConfigs(bidSectionConfigDomainService.saveBidSectionConfig(bidSectionConfigs
                , domain.getBidSectionId()
                , domain.getProjectId()
                , (data.getBidSectionId() != null)));

        return domain;
    }

    @Override
    public void deleteInfo(Long bidSectionId) {
        BidSection basic = bidSectionRepository.selectByBidSectionId(bidSectionId);
        if (basic == null) {
            throw LogicException.raise(CustomError.BID_SECTION_NOT_EXIST);
        } else {
            bidSectionConfigDomainService.cleanBidSectionConfig(bidSectionId);
            bidSectionRepository.doDeleteByBidSectionId(bidSectionId);
        }
    }

    @Override
    public Boolean checkProjectOrgBeUsedInBidSection(Long projectOrgId) {
        return bidSectionConfigOrganizationRepository.selectProjectOrgUsedInBidSectionCount(projectOrgId) > 0;
    }

    @Override
    public void deleteByProjectId(Long projectId) {
        bidSectionRepository.doDeleteByProjectId(projectId);
    }
}
